
//  Created by 莫瑞权 on 2022/5/9.
//  Copyright © 2022 Talkfun. All rights reserved.
//

import UIKit

class TFMultifunctionButton: TalkfunIncreaseButton, CAAnimationDelegate {
    @objc public var flower:Bool = false
    var loadingLayer:CALayer?
    var redDotLayer:CAShapeLayer?
    var redView:UIView?
    var prompt:UILabel?
    var Triangle:UIView?
    var numberLabel:UILabel?
    
    //画红点点
    @objc public func drawRedDot()
    {
        if(self.redDotLayer==nil){
            
            let frame = CGRect(x: 0, y: 0, width: self.frame.size.width/6, height: self.frame.size.width/6)
            self.redDotLayer = CAShapeLayer()
            // 指定frame，只是为了设置宽度和高度
            self.redDotLayer!.frame = frame;
            // 设置居中显示
            self.redDotLayer!.position = CGPoint(x: (self.frame.size.width/2)+(self.frame.size.width/2/2), y: ((self.frame.size.height/2)-(self.frame.size.height/2/2)))
            
            self.redDotLayer!.fillColor = UIColor.red.cgColor
            // 设置线宽
            self.redDotLayer!.lineWidth = 1
            // 设置线的颜色
            self.redDotLayer!.strokeColor = UIColor.red.cgColor
            // 使用UIBezierPath创建路径
            
            let circlePath = UIBezierPath.init(rect: frame)
            // 设置CAShapeLayer与UIBezierPath关联
            self.redDotLayer!.path = circlePath.cgPath
            self.redDotLayer!.strokeStart = 0.0
            self.redDotLayer!.strokeEnd =   1.0
            // 将CAShaperLayer放到某个层上显示
            self.layer.addSublayer(self.redDotLayer!)
        }
        
    }
    //清除小红点
    @objc public func clearRedDot()
     {
        if ((self.redDotLayer) != nil) {
            self.redDotLayer!.removeFromSuperlayer()
            self.redDotLayer  = nil
        }
    }
    func rotationAnimation()
    {
      let rotationAnimation = CABasicAnimation.init(keyPath: "transform.rotation.z")
        let toValue:CGFloat = .pi*2.0
        rotationAnimation.toValue = toValue
        rotationAnimation.duration = 2
        rotationAnimation.isCumulative = true
        rotationAnimation.repeatCount = 1
        rotationAnimation.delegate = self
        if ((self.redDotLayer) != nil) {
           self.loadingLayer!.add(rotationAnimation, forKey: "rotationAnimation")
        }
       
    }
    //动画结束时调用
    func animationDidStop(_ anim: CAAnimation, finished flag: Bool) {
        if ( self.loadingLayer != nil ) {
            self.rotationAnimation()
        }
    }
   
    override func layoutSubviews() {
        super.layoutSubviews()
        
        if (self.flower ) {
            /****按钮 ****/
            let imageViewW:CGFloat = self.frame.size.width;
            let imageViewY:CGFloat = 0
            let imageViewX:CGFloat  = 0
            self.imageView!.frame = CGRect(x: imageViewX, y: imageViewY, width: imageViewW, height: imageViewW)
            
            self.imageView!.backgroundColor = UIColor.clear
            self.imageView!.contentMode = .scaleAspectFit;
        }
    }
    @objc public func drawRedDotWidthNumber(_ number:String) {

        if (self.redView==nil){
            self.redView = UIView()
            self.addSubview(self.redView!)
            
            self.redView!.frame = CGRect(x: self.frame.size.width-7, y: -7, width: 14, height: 14)
            self.redView!.layer.borderColor = UIColor(red: 255/255.0, green: 255/255.0, blue: 255/255.0, alpha: 1).cgColor
            self.redView!.layer.borderWidth = 1
            self.redView!.backgroundColor = UIColor(red: 255/255.0, green: 65/255.0, blue: 65/255.0, alpha: 1)
        
            self.redView!.layer.cornerRadius = self.redView!.bounds.size.width/2
            
            self.numberLabel = UILabel()
            self.redView!.addSubview(self.numberLabel!)
            self.numberLabel!.textAlignment = .center
            self.numberLabel!.frame = self.redView!.bounds
            self.numberLabel!.numberOfLines = 0;
            self.numberLabel!.font = UIFont.PingFang(size: 12)
            self.numberLabel!.textColor = UIColor(red: 255/255.0, green: 255/255.0, blue: 255/255.0, alpha: 1)
          
            self.prompt = UILabel()
            self.addSubview(self.prompt!)
            self.prompt!.isHidden = true
            self.prompt!.textAlignment = .center
            self.prompt!.textColor = UIColor(red: 64/255.0 , green: 85/255.0, blue: 110/255.0, alpha: 1)
            self.prompt!.frame = CGRect(x: -(132.5/2)+(self.frame.size.width/2), y: -12.5-30, width: 132.5, height: 16.5+6)
            self.prompt!.backgroundColor = UIColor(red: 223/255.0, green: 230/255.0, blue: 238/255.0, alpha: 1)
            self.prompt!.layer.shadowColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.16).cgColor
           
            self.prompt!.layer.shadowOffset = CGSize(width: 0, height: 1.5)
            self.prompt!.layer.shadowRadius = 3
            self.prompt!.layer.cornerRadius = 3
            self.prompt!.layer.shadowOpacity = 1
            self.prompt!.layer.masksToBounds = true
           
            self.prompt!.textColor = UIColor(red: 64/255.0, green: 85/255.0, blue: 110/255.0, alpha: 1)
            self.prompt!.font = UIFont.PingFang(size: 12)
             
            
            self.Triangle = UIView()
            self.Triangle?.isHidden = true
            self.Triangle!.frame = CGRect(x: (self.frame.size.width/2)-4, y: self.prompt!.frame.maxY, width: 8, height: 8)
            self.addSubview(self.Triangle!)
            
            let path =  UIBezierPath()
            path.move(to: CGPoint(x: 0, y: 0))
            path.addLine(to: CGPoint(x: self.Triangle!.frame.size.width/2, y: self.Triangle!.frame.size.height))
            path.addLine(to: CGPoint(x: self.Triangle!.frame.size.width, y: 0))
            path.addLine(to:  CGPoint(x: 0, y: 0))
        
           let shapeLayer = CAShapeLayer()
           //填充色
            
           shapeLayer.fillColor = self.prompt!.backgroundColor?.cgColor
           shapeLayer.strokeColor = shapeLayer.fillColor
           shapeLayer.path = path.cgPath
            self.Triangle!.layer.addSublayer(shapeLayer)
            
         }
       
        if (number != "0" ) {
             self.redView!.isHidden = false
             self.numberLabel!.text = number;
        }else{
            self.redView!.isHidden = true
        }
    }
    @objc public func prompt(_ second:String)
    {

        if (second != nil && self.prompt != nil && self.Triangle != nil) {
            
            self.prompt!.isHidden = false
            self.Triangle!.isHidden = false
            
            
            self.prompt!.text = second + "秒后可获得一朵鲜花"

            DispatchQueue.main.asyncAfter(deadline: DispatchTime.now()+1.0) {
                self.prompt!.isHidden = true;
                 self.Triangle!.isHidden = true;
                }
          
        }
       
    }



}
